const {
	Service
} = require("uni-cloud-router");

let cheerio = require('cheerio'); //爬虫
let iconv = require('iconv-lite'); //处理gbk编码的网页
let Entities = require('html-entities').XmlEntities;
let entities = new Entities();

// const redis = uniCloud.redis()
const baseUrl = "http://www.xbiqugu.net/";
const baseEncode = "utf-8";
module.exports = class BookService extends Service {

	async getBookInfo() {
// 可以使用redis缓存
		// const datas = await redis.get('bookInfo_' + this.ctx.data.url);
		// if (datas && datas.length > 0) {
		// 	console.log('redis缓存获取')
		// 	return JSON.parse(datas);
		// }
		/**
		 * 请求内容数据
		 */
		const res = await uniCloud.httpclient.request(this.ctx.data.url, {
			method: 'GET',
			gzip: true,
			encoding: null,
			timeout: 10000,
			followRedirect: true
		})
		var body = res.data;
		body = iconv.decode(body, baseEncode); //转换gbk编码的页面内容
		var $ = cheerio.load(body);

		var book = {
			cover: $("#fmimg > img").attr("src"),
			autor: $("#info > p:nth-child(2)").text(),
			time: $("#info > p:nth-child(4)").text(),
			newTitle: $("#info > p:nth-child(5)").text(),
			desc: $("#intro").text(),
		}
// document.querySelector("#list > dl > dd:nth-child(12)")
		var cata = [];
		$("#list > dl > dd:nth-child(n+12) > a").each((index, element) => {
			if (index < 20) {
				var href = $(element).attr("href");
				var title = entities.decode($(element).text())
				cata.push({
					title: title,
					url: href
				});
			}
		})

		// if (cata && cata.length > 0) {

		// 	await redis.set('bookInfo_' + this.ctx.data.url, JSON.stringify({
		// 		book,
		// 		cata
		// 	}), 'EX', 24 * 3600)
		// }

		return {
			book,
			cata
		}
	}

	async getBookClassifyList() {
		// const datas = await redis.get('bookClassify_' + baseUrl + this.ctx.data.url);
		// if (datas && datas.length > 0) {
		// 	console.log('redis缓存获取')
		// 	return JSON.parse(datas);
		// }
		/**
		 * 请求内容数据
		 */
		const res = await uniCloud.httpclient.request(baseUrl + this.ctx.data.url, {
			method: 'GET',
			gzip: true,
			encoding: null,
			timeout: 10000,
			followRedirect: true
		})
		var body = res.data;
		body = iconv.decode(body, baseEncode); //转换gbk编码的页面内容
		var $ = cheerio.load(body);

		var hotPath = "#hotcontent > div.ll > div"

		/**
		 * 热门推荐
		 */
		var hot = [];
		$(hotPath).each((index, element) => {


			hot.push({
				title: $(element).find("dt > a").text(),
				url: $(element).find(".image > a").attr("href"),
				cover: $(element).find(".image > a > img").attr("src"),
			})

		})

		var last = [];
		var listPath = "#newscontent > div.l > ul > li";
		$(listPath).each((index, element) => {
			last.push({
				title: $(element).find(".s2 > a").text(),
				url: $(element).find(".s2 > a").attr("href"),
				autor: $(element).find(".s4").text(),
				newTitle: $(element).find(".s3 > a").text()
			})
		})

		var rank = [];
		var rankPath = "#newscontent > div.r > ul > li"
		$(rankPath).each((index, element) => {
			rank.push({
				title: $(element).find(".s2 > a").text(),
				url: $(element).find(".s2 > a").attr("href"),
				autor: $(element).find(".s5").text()
			})
		})

		// if (hot && hot.length > 0) {
		// 	await redis.set('bookClassify_' + baseUrl + this.ctx.data.url, JSON.stringify({
		// 		hot,
		// 		last,
		// 		rank
		// 	}), 'EX', 12 * 3600)
		// }


		return {
			hot,
			last,
			rank
		};
	}

	/**
	 * 获取书单列表
	 */
	async getBookList() {
		
	

		/**
		 * 请求内容数据
		 */
		const res = await uniCloud.httpclient.request(baseUrl, {
			method: 'GET',
			gzip: true,
			encoding: null,
			timeout: 10000,
			followRedirect: true
		})
		var body = res.data;
		body = iconv.decode(body, baseEncode); //转换gbk编码的页面内容
		var $ = cheerio.load(body);

		var hotPath = "#hotcontent > div.l > div"

		/**
		 * 热门推荐
		 */
		var hot = [];
		$(hotPath).each((index, element) => {


			hot.push({
				// title: $(element).find(".image > a").attr("title"),
				title: $(element).find("dt > a").text(),
				url: $(element).find(".image > a").attr("href"),
				cover: $(element).find(".image > a > img").attr("src"),
			})

		})

		//分类
		var list = [];
		var listPath = ".novelslist > div.content";
		$(listPath).each((index, element) => {
			var title = $(element).find("h2").text();
			var top = {
				title: $(element).find(".top > dl > dt > a").text(),
				url: $(element).find(".top > dl > dt > a").attr("href"),
				cover: $(element).find(".top > .image  > img").attr("src"),
				
				// url: $(element).find(".top > .image > a").attr("href"),
				// //.top > .image > a > img
				// cover: $(element).find(".top > .image  > img").attr("src"),
				desc: $(element).find(".top > dl > dd").html()
			};
			var children = [];

			$(element).find("li").each((i, e) => {
				children.push({
					title: $(e).find("a").text(),
					autor: $(e).text().replace($(e).find("a").text(), ""),
					url: $(e).find("a").attr("href")
				})
			});

			list.push({
				title: title,
				top: top,
				children: children
			})

		})
		//最新更新
		// document.querySelector("#newscontent > div.l")
		var newList = [];
		var newListPath = "#newscontent > div.l";
		$(newListPath).each((index, element) => {
			
		
			$(element).find("li").each((i, e) => {
				newList.push({
					type:$(e).find(".s1").text(),
					title:$(e).find(".s2 > a").text(),
					url:$(e).find(".s2 > a").attr("href"),
					new: $(e).find(".s3 > a").text(),
					author:$(e).find(".s4").text(),
					time:$(e).find(".s5").text()
				})
			});
		
		
		})
		//最新入库
		// document.querySelector("#newscontent > div.r")
		var newList2 = [];
		var newListPath2 = "#newscontent > div.r";
		$(newListPath2).each((index, element) => {
			
		
			$(element).find("li").each((i, e) => {
				newList2.push({
					type:$(e).find(".s1").text(),
					title:$(e).find(".s2 > a").text(),
					url:$(e).find(".s2 > a").attr("href"),
					time:$(e).find(".s5").text()
				})
			});
		
		
		})
		

		

		return {
			hot,
			list,
			newList,
			newList2
		}
	}
};
